/*	kd:
	These are UI sprites. Buttons are disassociated from sprites because we may
	have a lot of sprites that cannot be clicked. For example,
	the message box.
	
	Sprites are so few, but drawn in different ways, that it's good to be
	explicit about which draw function to call. They may all take different
	parameters. */

struct Gcs_UiSprites {
	Array <float>       m_sprite_start_x;
	Array <float>       m_sprite_start_y;
	Array <float>       m_sprite_end_x;
	Array <float>       m_sprite_end_y;
	Array <float>       m_sprite_x;
	Array <float>       m_sprite_y;
	Array <float>       m_sprite_old_x;
	Array <float>       m_sprite_old_y;
	Array <TextureId>   m_sprite;
	uint m_size;
	
	Gcs_LinearFades     m_offset_x;
	Gcs_LinearFades     m_offset_y;
	Gcs_LinearFades     m_light;
	Gcs_LinearFades     m_alpha;
	Array <float>       m_proc_light;    /* Sine pulsed version of m_light. */
	Array <float>       m_proc_offset_x; /* Same for offsets (bounce anim). */
	Array <float>       m_proc_offset_y; /* Same for offsets (bounce anim). */
	
	TextureId At (uint handle) {
		return m_sprite [handle];
	}
	
	uint AddSprite (Name sprite, float x1, float y1, float x2, float y2) {
		TextureId tex = TexMan.CheckForTexture(sprite, TexMan.Type_Any);
		
		if(!tex) {
			Console.printf("Gcs_UiSprites.Add(%s, ...): "
				"Couldn't find TextureId for that name. Didn't add UI Sprite.",
				sprite);
			return 0;
		}
		
		m_sprite_start_x.push(x1);
		m_sprite_start_y.push(y1);
		m_sprite_end_x.push(x2);
		m_sprite_end_y.push(y2);
		m_sprite_x.push(x1);
		m_sprite_y.push(y1);
		m_sprite_old_x.push(x1);
		m_sprite_old_y.push(y1);
		m_sprite.Reserve(1);		/* I'm not sure why I can't just push. */
		m_sprite [m_size] = tex;
		m_size++;
		
		m_offset_x.       Register();
		m_offset_y.       Register();
		m_light.          Register();
		m_alpha.          Register();
		m_proc_light.     Reserve(1);
		m_proc_offset_x.  Reserve(1);
		m_proc_offset_y.  Reserve(1);
		return m_size - 1;
	}
	
	void UpdatePositions (float t) {
		float t_inv = 1 - t;
		
		for(uint i = 0; i < m_size; i++) {
			m_sprite_old_x [i] = m_sprite_x [i];
			m_sprite_old_y [i] = m_sprite_y [i];
			
			m_sprite_x [i] = t_inv * m_sprite_start_x [i] + t * m_sprite_end_x [i];
			m_sprite_y [i] = t_inv * m_sprite_start_y [i] + t * m_sprite_end_y [i];
			
			m_sprite_x [i] += m_proc_offset_x [i];
			m_sprite_y [i] += m_proc_offset_y [i];
		}
	}
	
	void UpdateFaders () {
		m_light.    StepAllFaders();
		m_alpha.    StepAllFaders();
		m_offset_x. StepAllFaders();
		m_offset_y. StepAllFaders();
		
		/*	Pulse the light / offset faders. This handles the button lighting up
			and offsets. */
		
		for(uint i = 0; i < m_size; i++) {
			m_proc_light    [i] = Gc_PulseFunc.tpulse_unit(m_light.Current(i), 0.125);
			m_proc_offset_x [i] = Gc_PulseFunc.tpulse_unit(
				m_offset_x.Interpol(i), 0.1) * m_offset_x.Interval(i);
			m_proc_offset_y [i] = Gc_PulseFunc.tpulse_unit(
				m_offset_y.Interpol(i), 0.1) * m_offset_y.Interval(i);
		}
	}
	
	void Fade (uint handle, uint tics, float alpha) {
		if(handle < m_size) {
			m_alpha.Fade(handle, m_alpha.Current(handle), alpha, 1.0 / tics);
		}
	}
	
	void Flash (uint handle, uint tics) {
		if(handle < m_size) {
			m_light.Fade(handle, 0, 1, 1.0 / tics);
		}
	}
	
	void Bounce (uint handle, uint tics, float x, float y) {
		if(handle < m_size) {
			m_offset_x.Fade(handle, 0, x, 1.0 / tics);
			m_offset_y.Fade(handle, 0, y, 1.0 / tics);
		}
	}
	
	ui void DrawSprites (RenderEvent event, float base_alpha, float inv_scale = 0.666) const {
		float t       = event.fractic;
		float t_inv   = 1 - t;
		float height  = inv_scale * 720;
		float width   = height * Screen.GetAspectRatio();
		float x       = 0;
		float y       = 0;
		uint i;
		
		for(i = 0; i < m_size; i++) {
			x = (t * m_sprite_x [i] + t_inv * m_sprite_old_x [i]) * width;
			y = (t * m_sprite_y [i] + t_inv * m_sprite_old_y [i]) * height;
			
			Screen.DrawTexture(m_sprite [i], true,
				x,
				y,
				DTA_KEEPRATIO, true,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_CENTEROFFSET, true,
				DTA_ALPHA, base_alpha * m_alpha.Current(i));
			
			Screen.DrawTexture(m_sprite [i], true,
				x,
				y,
				DTA_KEEPRATIO, true,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_CENTEROFFSET, true,
				DTA_ALPHA, m_alpha.Current(i));
			
			Screen.DrawTexture(m_sprite [i], true,
				x,
				y,
				DTA_KEEPRATIO, true,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_LEGACYRENDERSTYLE, STYLE_ADD,
				DTA_CENTEROFFSET, true,
				DTA_ALPHA, m_proc_light [i]);
		}
	}
}
